package test07;

import java.util.Stack;

class Solution {
    public int evalRPN(String[] tokens) {
        int len = tokens.length;
        Stack<Integer> stack = new Stack<>();
        for(int i = 0; i < len; i++){
            if(tokens[i].equals("*")){
                int temp1 = stack.pop();
                int temp2 = stack.pop();
                stack.push(temp2 * temp1);
            }else if(tokens[i].equals("+")){
                int temp1 = stack.pop();
                int temp2 = stack.pop();
                stack.push(temp2 + temp1);
            }
            else if(tokens[i].equals("-")){
                int temp1 = stack.pop();
                int temp2 = stack.pop();
                stack.push(temp2 - temp1);
            }
            else if(tokens[i].equals("/")){
                int temp1 = stack.pop();
                int temp2 = stack.pop();
                stack.push(temp2 / temp1);
            }
            else {
                stack.push(Integer.valueOf(tokens[i]));
            }
        }
        return stack.pop();

    }
}